package com.swanclouds.api.types;

import java.util.regex.Pattern;

public enum ServiceType {

    SHARED("shared","shared"),
    IDENTITY("identity", "Sessions"),
    SYSTEMS("Systems", "Systems"),
    CHASSIS("Chassis", "Chassis"),
    MANAGERS("Managers", "Managers"),
    UPDATES("Updates", "Updates"),
    ACCOUNTS("AccountService", "AccountService"),
    TASKS("TaskService","TaskService"),
    UNKNOWN("NA", "NA");

    private static final String SERVICE_PATTERN_SUFFIX = "[v|\\d|\\.]*";
    private final String serviceName;
    private final String type;
    private final Pattern servicePattern;

    ServiceType(String serviceName, String type) {
        this.serviceName = serviceName;
        this.type = type;
        this.servicePattern = Pattern.compile(Pattern.quote(serviceName) + SERVICE_PATTERN_SUFFIX +
                        "|" + Pattern.quote(type) + SERVICE_PATTERN_SUFFIX +
                        "|" + Pattern.quote(this.name()) + SERVICE_PATTERN_SUFFIX
                , Pattern.CASE_INSENSITIVE);
    }

    public static ServiceType forName(String name) {
        if (name == null || name.isEmpty()) {
            return ServiceType.UNKNOWN;
        }
        for (ServiceType s : ServiceType.values()) {
            if (s.getServicePattern().matcher(name).matches())
                return s;
        }
        return ServiceType.UNKNOWN;
    }

    public String getServiceName() {
        return this.serviceName;
    }

    public String getType() {
        return this.type;
    }

    private Pattern getServicePattern() {
        return this.servicePattern;
    }
}
